package cn.goodscript.web.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.lucene.document.Document;

import cn.goodscript.lucence.LucenceUtil;

/**
 * 所有ext与后台数据的请求都是由该Servlet来处理 前台必须传递一个mothed参数 该参数指定执行的方法名
 * 处理ext请求的这些方法格式都必须是:public String xxx(HttpServletRequest request)
 * 
 * @author 197370
 * 
 */
public class ExtJsonServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6039228711494931978L;

	/**
	 * 
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/json; charset=UTF-8");
		String mothed = request.getParameter("mothed");
		Object obj = null;
		// 根据参数mothed来判断执行的方法
		try {
			Method m = this.getClass().getMethod(mothed, HttpServletRequest.class);
			obj = m.invoke(this, request);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		response.getWriter().write(obj.toString());
		response.flushBuffer();
	}

	public String doSearch(HttpServletRequest request) {
		String saerchText = request.getParameter("saerchText");
		List<Document> listDocs = LucenceUtil.searchFiles("extjs", "title", saerchText);
		JSONArray dd = JSONArray.fromObject(listDocs);
		return dd.toString();
	}

	public String getData() {
		return null;
	}

	/**
	 * 处理树结构
	 * 
	 * @param request
	 * @return
	 */
	public String treeloader(HttpServletRequest request) {
		String path = request.getParameter("path");
		List<Node> list = new ArrayList<Node>();
		File file = new File(path);

		if (file.isDirectory()) {
			File[] listFiles = file.listFiles();
			for (File f : listFiles) {
				Node node = new Node();
				node.setId(f.getAbsolutePath());
				node.setText(f.getName());
				node.setLeaf(!f.isDirectory());
				list.add(node);
			}
		}
		JSONArray dd = JSONArray.fromObject(list);

		return dd.toString();
	}

	public String getFileContent(HttpServletRequest request) {
		String filePath = request.getParameter("url");
		if (filePath.equals("")) {
			return "";
		}
		File file = new File(filePath);
		String text = "";
		if (file.exists()) {
			try {
				FileInputStream fis = new FileInputStream(file);
				byte[] b = new byte[fis.available()];
				fis.read(b);
				text = new String(b, "UTF-8");
				fis.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return text;
	}

	public String isexist(HttpServletRequest request) {
		String filePath = request.getParameter("filePath");
		File file = new File(filePath);
		if (file.exists()) {
			return "succ";
		}
		return "failure";
	}

	public String createRandomCode(HttpServletRequest request) {
		long currentMillis = System.currentTimeMillis();
		String randomCode = CompressEncodeing.CompressNumber(currentMillis, 6);
		return randomCode;
	}

	/**
	 * 删除文件
	 * 
	 * @param request
	 * @return
	 */
	public String delFile(HttpServletRequest request) {
		String filePath = request.getParameter("filePath");

		File file = new File(filePath);
		if (file.exists()) {
			file.delete();
		}
		return "succ";
	}

	/**
	 * 重新命名文件名
	 * 
	 * @param request
	 * @return
	 */
	public String renameFile(HttpServletRequest request) {
		String newpath = request.getParameter("newPath");
		String oldpath = request.getParameter("oldPath");

		File file = new File(oldpath);
		if (file.exists()) {
			file.renameTo(new File(newpath));
		}
		return "succ";
	}

	/**
	 * 同步页面上的源码到文件中
	 * 
	 * @param request
	 * @return
	 */
	public String saveFile(HttpServletRequest request) {
		String filePath = request.getParameter("filePath");
		String code = request.getParameter("code");

		File file = new File(filePath);
		try {
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(code.getBytes("UTF-8"));
			fos.flush();
			fos.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "succ";
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
}
